/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-12
 * @Time  : 下午9:37
 * @brief :
 */
(function () {
    var util = require("common:widget/ui/libs/util.js").util,
        Check = require('common:widget/ui/base/check.js').Check,
        $page = $('.page-login'),
        $phone = $page.find('.phone'),
        $password = $page.find('.password'),
        $errorTip = $page.find('.error-tip'),
        $btnConfirm = $page.find('.btn-confirm'),
        $qcode = $page.find('.qcode'),
        param = util.getUrlParas(),
        from = param['zrb_from']?param['zrb_from']:'/user/wallet',
        check;

    if(util.readCookie("zhenrongbao_user_name")){
        $phone.val(util.readCookie("zhenrongbao_user_name"));
    }

    check = new Check({
        '$phone':$phone,
        '$password':$password,
        '$tip':$errorTip,
        '$qcode':$qcode
    });

    $btnConfirm.click(function(){
        var me = this;
        $errorTip.css('visibility','hidden');
        if(!$(this).hasClass('btn-disable')){
            if(check.check()){
                util.setCookie("zhenrongbao_user_name="+$.trim($phone.val()));
                $(me).addClass('btn-disable');
                $.post('/account/loginusernew',{
                    'user_name': $.trim($phone.val()),
                    'passwd': $.trim($password.val()),
                    'qcode': $.trim($qcode.val())
                },function(ret,status){
                    if(ret.error_no == 0){
                        location.href=from;
                    }else{
                        $errorTip.css('visibility','visible').text(ret.error_message);
                        $(me).removeClass('btn-disable');
                        $('.widget-gcode .btn-refresh').trigger('click');
                        $qcode.val('');
                    }
                },'json');
            }
        }
    });


    $phone.on('keydown',function(e){
        if(e.keyCode == 13){
            $btnConfirm.trigger('click');
        }
    });

    $password.on('keydown',function(e){
        if(e.keyCode == 13){
            $btnConfirm.trigger('click');
        }
    });

    $qcode.on('keydown',function(e){
        if(e.keyCode == 13){
            $btnConfirm.trigger('click');
        }
    });

})();